package com.houzhiwen.util;

import com.houzhiwen.javabeam.Role;
import mapper.RoleMapper;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.List;

public class RoleMapperTest {
    SqlSession session;
    RoleMapper mapper;

    @Before
    public void before() {
        //1、创建SqlSessionFactory对象，也是单例模式的
        SqlSessionFactory factory = MyBatisUtil.getSqlSessionFactory();
        //2、创建SqlSession对象 true 自动提交事务 不写也没事 因为默认在sqlsession关闭时提交事务
        session = factory.openSession(true);
        //3、调用session的方法namespace的名字.id
        mapper = session.getMapper(RoleMapper.class);

    }

    @After
    public void after() {
        //5、关闭资源
        session.close();
    }

    @Test
    public void selectRole_Permission(){
        for (Role role : mapper.selectRole_Permission()) {
            System.out.println(role);
        }
    }

//    延迟加载
@Test
public void selectRole_Permission_lazy(){
    List<Role> roles = mapper.selectRole_Permission_lazy();
    //方法执行默认查询主表数据并返回
    //当使用主表数据时 可以直接使用
    Role role = roles.get(0);
    System.out.println(role.getRid());
    System.out.println(role.getName());
    System.out.println(role.getDesc());
    System.out.println("~~~~~~~~~~~~~~~~~~~~");
    //当使用关联表数据时 会根据当前主表数据进行查询
    System.out.println(role.getPlist());

    System.out.println("=========================");
    Role role1 = roles.get(1);
    System.out.println(role1.getRid());
    System.out.println(role1.getName());
    System.out.println(role1.getDesc());
    System.out.println("~~~~~~~~~~~~~~~~~~~~");
    //当使用关联表数据时 会根据当前主表数据进行查询
    System.out.println(role1.getPlist());
}
}
